package net.huashitong.Indicators.service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import com.sirdc.modules.sys.service.SysTableService;
import net.huashitong.Indicators.DTO.IndicatorModelData;
import net.huashitong.Indicators.dao.IndicatorBreakdownDao;
import net.huashitong.Indicators.databean.IndicatorBreakdownDatabean;
import net.huashitong.Indicators.entity.IndicatorBreakdown;
import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.filter.IndicatorRank;
import net.huashitong.scheduling.web.RevenueController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.utils.ObjectUtils;

@Service
public class IndicatorBreakdownService extends StringPKService<IndicatorBreakdown> {

    @Autowired
    private IndicatorBreakdownDao dao;
    public static final String[] months = {"12", "11", "10", "09", "08", "07", "06", "05", "04", "03", "02", "01"};
    public static final String[] years = {"2022", "2021", "2020", "2019", "2018", "2017", "2016", "2015"};
    private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private DecimalFormat dfs = new DecimalFormat("##.##");

    @Autowired
    private SysTableService tableService;

    @Override
    protected void beforeSave(IndicatorBreakdown indicatorBreakdown) {
        indicatorBreakdown.setSysId(tableService.updateMaxSysId("t_gis_indicatorBreakdown", null));
    }

    @Override
    protected BaseDao<IndicatorBreakdown, String> getDao() {
        return dao;
    }

    public List<IndicatorBreakdown> queryByCuMonthAndArea(
            String currentMonth, String area) {
        return dao.queryByCuMonthAndArea(currentMonth, area);
    }

    public List<IndicatorBreakdown> queryCountyRank(
            IndicatorBreakdownFilter filter) {
        return dao.queryByCuMonthAndArea(filter);
    }

    public List<IndicatorBreakdown> queryByCuMonthAndAreaOR(
            IndicatorBreakdownFilter filter) {
        return dao.queryByCuMonthAndAreaOR(filter);
    }

    public List<IndicatorBreakdown> queryDevelopmentSituation(
            IndicatorBreakdownFilter filter) {
        return dao.queryDevelopmentSituation(filter);
    }

    public List<IndicatorBreakdown> queryAbsoluteValue(
            IndicatorBreakdownFilter filter) {
        return dao.queryAbsoluteValue(filter);
    }

    public List<Map<String, Object>> queryAll(String date, String industryName, String area) {
        return dao.queryAll(date, industryName, area);
    }

    public List<IndicatorBreakdown> queryMonthAbsolute(
            IndicatorBreakdownFilter filter) {
        return dao.queryMonthAbsolute(filter);
    }

    public List<IndicatorBreakdown> queryMonthDevelopmentSituation(
            IndicatorBreakdownFilter filter) {
        return dao.queryMonthDevelopmentSituation(filter);
    }

    public List<IndicatorBreakdown> queryShopping(
            IndicatorBreakdownFilter filter) {
        return dao.queryShopping(filter);
    }

    public List<IndicatorBreakdown> queryShoppingInd(
            IndicatorBreakdownFilter filter) {
        return dao.queryShoppingInd(filter);
    }

    public List<String> queryCountyRankArea(IndicatorBreakdownFilter filter) {
        return dao.queryCountyRankArea(filter);
    }

    public List<String> queryAllDateTimes() {
        return dao.queryAllDateTimes();
    }

    public IndicatorBreakdown queryMonthValue(IndicatorBreakdownFilter filter) {
        return dao.queryMonthValue(filter);
    }

    public List<IndicatorBreakdown> queryMonthValueSec(
            IndicatorBreakdownFilter filter) {
        return dao.queryMonthValueSec(filter);
    }

    public IndicatorBreakdown queryByDateAndId(
            IndicatorBreakdown indicatorBreakdown) {
        return dao.queryByDateAndId(indicatorBreakdown);
    }

    public List queryAllSec(String date, String industryName, String area) {
        return dao.queryAllSec(date, industryName, area);
    }

    public List<Map<String, Object>> queryByIndustryId(String industryId) {

        List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
        for (int j = 0; j < IndicatorBreakdownService.years.length; j++) {
            for (int i = 0; i < IndicatorBreakdownService.months.length; i++) {
                List<IndicatorBreakdown> list = dao.queryByIndustryId(industryId, years[j] + months[i]);
                if (list.size() > 3) {
                    int s = 1;
                    for (IndicatorBreakdown ib : list) {
                        if (!"福州市".equals(ib.getArea())) {
                            Map<String, Object> map = new HashMap<>();
                            map.put("Time", df.format(new Date()));
                            map.put("Type", ib.getIndustryName());
                            if ("A09".equals(ib.getIndustryId()) || "A10".equals(ib.getIndustryId()) || "A18".equals(ib.getIndustryId()) || "A19".equals(ib.getIndustryId())) {
                                map.put("AreaName", ib.getArea().substring(0, ib.getArea().length() - 1));
                            } else {
                                map.put("AreaName", ib.getArea());
                            }
                            map.put("Ranking", s);
		  					 /*if(ObjectUtils.isNotBlank(ib.getCumulative())){
		  						 map.put("ProductValue", dfs.format(Double.parseDouble(ib.getCumulative())/10000));
		  					 }else{
		  						 map.put("ProductValue","0");
		  					 }*/
                            if (ObjectUtils.isNotBlank(ib.getRatio())) {
                                map.put("ProductValue", ib.getRatio());
                            } else {
                                map.put("ProductValue", "0");
                            }
                            lists.add(map);
                            s++;
                        }

                    }
                    return lists;
                }
            }
        }
        return lists;
    }

    public Map<String, Object> queryIndicatorBreakdownByIndustryId(String industryId) {
        Map<String, Object> map = new HashMap<>();
        for (int j = 0; j < IndicatorBreakdownService.years.length; j++) {
            for (int i = 0; i < IndicatorBreakdownService.months.length; i++) {
                IndicatorBreakdown in = dao.queryIndicatorBreakdownByIndustryId(industryId, years[j] + months[i], "永泰县");
                if (ObjectUtils.isNotBlank(in) && !"0".equals(in.getCumulative()) && !"0".equals(in.getRatio())) {
                    map.put("Time", df.format(new Date()));
                    map.put("Type", in.getIndustryName());
                    if (ObjectUtils.isNotBlank(in.getCumulative())) {
                        map.put("ComulativeCompletionValue", dfs.format(Double.parseDouble(in.getCumulative()) / 10000));
                    } else {
                        map.put("ComulativeCompletionValue", "0");
                    }
                    map.put("Growth", in.getRatio());
                    return map;
                }
            }
        }
        return map;
    }

    public String getNewDate() {
        return dao.getNewDate();
    }

    public List<IndicatorBreakdown> queryIndicatorBreakdowns(String area, String industryId, String date) {
        return dao.queryIndicatorBreakdowns(area, industryId, date);
    }

    public List<IndicatorRank> getRank3(String industryId, String date) {
        return dao.getRatioRank(industryId, date);
    }

    public IndicatorBreakdown getIndicatorDateTime(String indicatorId) {
        return dao.getIndicatorDateTime(indicatorId);
    }

    public IndicatorBreakdown getIndicatorDateTimeByName(String indicatorName) {
        return dao.getIndicatorDateTimeByName(indicatorName);
    }

    public List<Map<String, Object>> queryAllDataByYear(String year, String area) {
        List<Map<String, Object>> result = new ArrayList<>();
        List<IndicatorBreakdown> list = dao.queryAllDataByYear(year, area);
        Map<String, List<IndicatorBreakdown>> map = new TreeMap<>();

        for (IndicatorBreakdown index : list) {
            if (!map.containsKey(index.getCurrentMonth())) {
                List<IndicatorBreakdown> indicatorList = new ArrayList<>();
                indicatorList.add(index);
                map.put(index.getCurrentMonth(), indicatorList);
            } else {
                List<IndicatorBreakdown> indicatorList = map.get(index.getCurrentMonth());
                indicatorList.add(index);
                map.put(index.getCurrentMonth(), indicatorList);
            }
        }
        for (String key : map.keySet()) {
            List<IndicatorModelData> dataList = new ArrayList<>();
            List<IndicatorBreakdown> lists = map.get(key);//
            for (IndicatorBreakdown indicatorBreakdown : lists) {
                IndicatorModelData data = new IndicatorModelData();
                data.setArea(indicatorBreakdown.getArea());
                data.setCumulative(indicatorBreakdown.getCumulative());
                data.setRatio(indicatorBreakdown.getRatio());
                data.setCurrentMonth(indicatorBreakdown.getCurrentMonth());
                data.setIndustryId(indicatorBreakdown.getIndustryId());
                data.setIndustryName(indicatorBreakdown.getIndustryName());
                if ("A18".equals(indicatorBreakdown.getIndustryId())
                        || "A19".equals(indicatorBreakdown.getIndustryId())) {
                    data.setUnit("元");
                } else {
                    data.setUnit("万元");
                }
                dataList.add(data);
            }

            Map<String, Object> map1 = new HashMap<>();
            for (IndicatorModelData data : dataList) {
                if (!map1.containsKey(data.getIndustryId())) {
                    List<IndicatorModelData> dataList1 = new ArrayList<>();
                    dataList1.add(data);
                    map1.put(data.getIndustryId(), dataList1);
                } else {
                    List<IndicatorModelData> dataList1 = (List<IndicatorModelData>) map1.get(data.getIndustryId());
                    dataList1.add(data);
                    map1.put(data.getIndustryId(), dataList1);
                }
            }
            List<Map<String, Object>> resList = new ArrayList<>();
            for (String dataKey : map1.keySet()) {
                Map<String, Object> map3 = new TreeMap<>();
                List<IndicatorModelData> dataLists = (List<IndicatorModelData>) map1.get(dataKey);
                List<IndicatorModelData> res = new ArrayList<>();
                for (IndicatorModelData indicatorBreakdown : dataLists) {
                    res.add(indicatorBreakdown);
                }
                map3.put("industryId",dataKey);
                map3.put("industryName",dataLists.get(0).getIndustryName());
                map3.put("data",res);
                resList.add(map3);
            }

            Map<String, Object> map2 = new HashMap<>();
            map2.put("time", key);
            map2.put("data", resList);
            result.add(map2);
        }

        return result;
    }


}




